package application.common.util;

public class Convert {
	public static String ToString(byte b, int toBase) {
		 return "" +(byte)((b >> 7) & 0x1) +   
				    (byte)((b >> 6) & 0x1) +   
				    (byte)((b >> 5) & 0x1) +   
				    (byte)((b >> 4) & 0x1) +   
				    (byte)((b >> 3) & 0x1) +   
				    (byte)((b >> 2) & 0x1) +   
				    (byte)((b >> 1) & 0x1) +   
				    (byte)((b >> 0) & 0x1); 
	}

	/**
	 * @功能 String左对齐
	 */
	public static String PadLeft(String src, int len, char ch) {
		int diff = len - src.length();
		if (diff <= 0) {
			return src;
		}

		char[] charr = new char[len];
		System.arraycopy(src.toCharArray(), 0, charr, 0, src.length());
		for (int i = src.length(); i < len; i++) {
			charr[i] = ch;
		}
		return new String(charr);
	}

	/**
	 * @功能 String右对齐
	 */
	public static String PadRight(String src, int len, char ch) {
		int diff = len - src.length();
		if (diff <= 0) {
			return src;
		}

		char[] charr = new char[len];
		System.arraycopy(src.toCharArray(), 0, charr, diff, src.length());
		for (int i = 0; i < diff; i++) {
			charr[i] = ch;
		}
		return new String(charr);
	}

	/**
	 * int到byte[] 由高位到低位
	 * 
	 * @param i 需要转换为byte数组的整行值。
	 * @return byte数组
	 */
	public static byte[] ToByte(int i) {
		byte[] result = new byte[4];
		result[0] = (byte) ((i >> 24) & 0xFF);
		result[1] = (byte) ((i >> 16) & 0xFF);
		result[2] = (byte) ((i >> 8) & 0xFF);
		result[3] = (byte) (i & 0xFF);
		return result;
	}

	/**
	 * byte[]转int
	 * 
	 * @param bytes 需要转换成int的数组
	 * @return int值
	 */
	public static int byteArrayToInt(byte[] bytes) {
		int value = 0;
		for (int i = 0; i < 4; i++) {
			int shift = (3 - i) * 8;
			value += (bytes[i] & 0xFF) << shift;
		}
		return value;
	}

	public static char[] reverse(String s) {
		return new StringBuffer(s).reverse().toString().toCharArray();
	}
}
